package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.GroupResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.NiceRecordMapper;
import com.ruoyi.system.domain.NiceRecord;
import com.ruoyi.system.service.INiceRecordService;

/**
 * 靓号记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-03
 */
@Service
public class NiceRecordServiceImpl implements INiceRecordService 
{
    @Autowired
    private NiceRecordMapper niceRecordMapper;

    /**
     * 查询靓号记录
     * 
     * @param id 靓号记录主键
     * @return 靓号记录
     */
    @Override
    public NiceRecord selectNiceRecordById(Long id)
    {
        return niceRecordMapper.selectNiceRecordById(id);
    }

    /**
     * 查询靓号记录列表
     * 
     * @param niceRecord 靓号记录
     * @return 靓号记录
     */
    @Override
    public List<NiceRecord> selectNiceRecordList(NiceRecord niceRecord)
    {
        List<NiceRecord> niceRecords = niceRecordMapper.selectNiceRecordList(niceRecord);
        //根据项目类别进行分类
        Map<Long,List<NiceRecord>> typeMap = niceRecords.stream().collect(Collectors.groupingBy(NiceRecord::getNumberType));
        List<NiceRecord> list = new ArrayList<>();
        //处理单张数据
        List<NiceRecord> banknoteSerialNumberRecords1 = typeMap.get(0L);
        if (banknoteSerialNumberRecords1 != null){
            banknoteSerialNumberRecords1.forEach(data -> {
                data.setIds(Collections.singletonList(data.getId()));
            });
            list.addAll(banknoteSerialNumberRecords1);
        }

        //处理标十数据
        //按大小排序
        niceRecords.sort(Comparator.comparing(NiceRecord::getSerialNumber));
        List<NiceRecord> banknoteSerialNumberRecords2 = typeMap.get(1L);
        if (banknoteSerialNumberRecords2 != null){
            //帮我找出每个十相连的数据
            //根据类别分组
            Map<Integer, List<NiceRecord>> typeMap1 = banknoteSerialNumberRecords2.stream().collect(Collectors.groupingBy(NiceRecord::getBillType));
            typeMap1.forEach((key,value)->{
                //排序
                value.sort(Comparator.comparing(NiceRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumbersN(value);
                for (GroupResult groupResult : groupResults) {
                    NiceRecord firstObject = groupResult.getNiceRecord();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getNumberRecords().stream().map(NiceRecord::getId).collect(Collectors.toList()));
                    list.add(firstObject);
                }
            });

        }

        //处理标百数据
        niceRecords.sort(Comparator.comparing(NiceRecord::getSerialNumber));
        List<NiceRecord> banknoteSerialNumberRecords3 = typeMap.get(2L);
        if (banknoteSerialNumberRecords3 != null){
            //分组
            Map<Integer, List<NiceRecord>> typeMap1 = banknoteSerialNumberRecords3.stream().collect(Collectors.groupingBy(NiceRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(NiceRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumbersTensN(value);
                for (GroupResult groupResult : groupResults) {
                    NiceRecord firstObject = groupResult.getNiceRecord();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getNumberRecords().stream().map(NiceRecord::getId).collect(Collectors.toList()));
                    list.add(firstObject);
                }
            });

        }


        List<NiceRecord> banknoteSerialNumberRecords4 = typeMap.get(3L);
        if (banknoteSerialNumberRecords4 != null){
            //分组
            Map<Integer, List<NiceRecord>> typeMap1 = banknoteSerialNumberRecords4.stream().collect(Collectors.groupingBy(NiceRecord::getBillType));
            typeMap1.forEach((key,value)->{
                value.sort(Comparator.comparing(NiceRecord::getSerialNumber));
                List<GroupResult> groupResults = groupSerialNumberssN(value);
                for (GroupResult groupResult : groupResults) {
                    NiceRecord firstObject = groupResult.getNiceRecord();
                    firstObject.setSerialNumber(groupResult.getGroupedNumber());
                    firstObject.setIds(groupResult.getNumberRecords().stream().map(NiceRecord::getId).collect(Collectors.toList()));
                    list.add(firstObject);
                }
            });

        }

        return list;
    }


    public List<GroupResult> groupSerialNumberssN(List<NiceRecord> objects) {
        List<GroupResult> groupedResults = new ArrayList<>();
        if (objects.isEmpty()) {
            return groupedResults;
        }
        int startIndex = 0;
        while (startIndex < objects.size()) {
            int endIndex = startIndex;
            while (endIndex + 1 < objects.size()) {
                String current = objects.get(endIndex).getSerialNumber();
                String next = objects.get(endIndex + 1).getSerialNumber();
                String currentPrefix = current.substring(0, current.length() - 2);
                String nextPrefix = next.substring(0, next.length() - 2);
                int currentSuffix = Integer.parseInt(current.substring(current.length() - 2));
                int nextSuffix = Integer.parseInt(next.substring(next.length() - 2));
                if (!currentPrefix.equals(nextPrefix) || nextSuffix != currentSuffix + 1) {
                    break;
                }
                endIndex++;
            }
            if (endIndex >= startIndex) {
                String start = objects.get(startIndex).getSerialNumber();
                String end = objects.get(endIndex).getSerialNumber();
                String groupedNumber = start + "-" + end.substring(end.length() - 2);
                groupedResults.add(new GroupResult(groupedNumber, objects.get(startIndex), objects.subList(startIndex, endIndex + 1)));
            }
            startIndex = endIndex + 1;
        }
        return groupedResults;
    }

    public List<GroupResult> groupSerialNumbersTensN(List<NiceRecord> banknoteSerialNumberRecords3) {
        //按照类型分类
        List<GroupResult> groupedResults = new ArrayList<>();

        Map<Integer, List<NiceRecord>> typeMap = banknoteSerialNumberRecords3.stream().collect(Collectors.groupingBy(NiceRecord::getBillType));
        typeMap.forEach((key, value) -> {
            //value进行排序 每10位 间隔取
            value.sort(Comparator.comparing(NiceRecord::getSerialNumber));
            for (int i = 0; i < value.size(); i += 100) {
                int end = Math.min(i + 100, value.size());
                String firstSerialNumber = value.get(i).getSerialNumber();
                String lastSerialNumber = value.get(end - 1).getSerialNumber();
                String newSerialNumber = firstSerialNumber + "-" + lastSerialNumber.substring(lastSerialNumber.length() - 3);
                groupedResults.add(new GroupResult(newSerialNumber, value.get(i),value.subList(i, end)));
            }

        });

        return groupedResults;

    }

    public List<GroupResult> groupSerialNumbersN(List<NiceRecord> banknoteSerialNumberRecords2) {
        //按照类型分类
        List<GroupResult> groupedResults = new ArrayList<>();

        Map<Integer, List<NiceRecord>> typeMap = banknoteSerialNumberRecords2.stream().collect(Collectors.groupingBy(NiceRecord::getBillType));
        typeMap.forEach((key, value) -> {
            //value进行排序 每10位 间隔取
            value.sort(Comparator.comparing(NiceRecord::getSerialNumber));
            for (int i = 0; i < value.size(); i += 10) {
                int end = Math.min(i + 10, value.size());
                String firstSerialNumber = value.get(i).getSerialNumber();
                String lastSerialNumber = value.get(end - 1).getSerialNumber();
                String newSerialNumber = firstSerialNumber + "-" + lastSerialNumber.substring(lastSerialNumber.length() - 2);
                groupedResults.add(new GroupResult(newSerialNumber, value.get(i), value.subList(i, end)));
            }
        });
        return groupedResults;
    }
    /**
     * 新增靓号记录
     * 
     * @param niceRecord 靓号记录
     * @return 结果
     */
    @Override
    public int insertNiceRecord(NiceRecord niceRecord)
    {
        return niceRecordMapper.insertNiceRecord(niceRecord);
    }

    /**
     * 修改靓号记录
     * 
     * @param niceRecord 靓号记录
     * @return 结果
     */
    @Override
    public int updateNiceRecord(NiceRecord niceRecord)
    {
        return niceRecordMapper.updateNiceRecord(niceRecord);
    }

    /**
     * 批量删除靓号记录
     * 
     * @param ids 需要删除的靓号记录主键
     * @return 结果
     */
    @Override
    public int deleteNiceRecordByIds(Long[] ids)
    {
        return niceRecordMapper.deleteNiceRecordByIds(ids);
    }

    /**
     * 删除靓号记录信息
     * 
     * @param id 靓号记录主键
     * @return 结果
     */
    @Override
    public int deleteNiceRecordById(Long id)
    {
        return niceRecordMapper.deleteNiceRecordById(id);
    }

    @Override
    public int updateByIds(List<Long> ids, Integer status, Integer price) {
        return niceRecordMapper.updateByIds(ids,status,price);
    }

    @Override
    public void deleteBanknoteSerialNumberBySerialNumbers(List<String> collect, Long key) {
        niceRecordMapper.deleteBanknoteSerialNumberBySerialNumbers(key, collect);
    }
}
